from django.shortcuts import render
from datetime import date, datetime
import json
# from django.views.decorators.csrf import requires_csrf_token, csrf_exempt
# from django.contrib.auth.decorators import login_required

# Create your views here.


class ComplexEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, datetime):
            return obj.strftime('%Y-%m-%d %H:%M:%S')
        elif isinstance(obj, date):
            return obj.strftime('%Y-%m-%d')
        else:
            return json.JSONEncoder.default(self, obj)


def get_menu(request):
    """
    通过menuid确定要激活的菜单按钮
    该方法是继承base.html后必须传入的参数，否则不会显示菜单栏
    :param request:
    :return:
    """
    from base.menu import menu
    # selected_menu_id = "312"
    selected_menu_id = request.GET.get("menuid", "111")
    if not selected_menu_id.isdigit() or len(selected_menu_id) != 3:
        selected_menu_id = "111"
    topmenuId = list(selected_menu_id)[0]
    childmenuId = "".join(list(selected_menu_id)[0:2]) + "0"
    return {"menu": menu, "topmenuId": topmenuId, "childmenuId": childmenuId, "sunmenuId": selected_menu_id}


